package com.cuz.daileetcode.bat100;

public class 绳子覆盖最多的点数 {
    public static void main(String[] args) {
        int[] arr = {0, 2, 3, 4, 7, 9, 15};
        int i = solution1(arr, 10);
        int i1 = solution2(arr, 10);
        System.out.println(i);
        System.out.println(i1);
    }

    public static int solution1(int[] arr, int len) {
        if (arr == null || arr.length <= 1) {
            return arr == null ? 0 : arr.length;
        }
        int left = 0;
        int right = 0;
        int ans = 1;
        while (right < arr.length) {
            right++;
            while (right < arr.length && left < right && arr[right] - arr[left] > len) {
                left++;
            }
            ans = Math.max(right - left + 1, ans);
        }
        return ans;
    }

    public static int solution2(int[] arr, int len) {
        if (arr == null || arr.length <= 1) {
            return arr == null ? 0 : arr.length;
        }
        int length = arr.length;
        int left = 0;
        int right = 0;
        int ans = 1;
        while (left < length) {
            while (right < length && arr[right] - arr[left] <= len) {
                right++;
            }
            ans = Math.max(ans, right - left);
            left++;
        }
        return ans;
    }
}
